<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改</title>
</head>
<body>
商品名称：<input type="text" id="name"><div id="name_msg" style="color: red"></div><br/>
价格：<input type="text" id="price">只支持整数<br>
图片：<input type="file" id="image"><input type="button" onclick="upload();" value="上传" ><br/>
<div><img src="" id="img"/></div>
描述：<textarea rows="" cols="" id="descrip"></textarea><br/>
产品特色：<input type="text" id="tags">多个特色用逗号分隔<br/>


服务保障：<br/>
<input type="checkbox" name="service" value="1">无购物<br/>
<input type="checkbox" name="service" value="2">成团保障<br/>
<input type="checkbox" name="service" value="3">安心游<br/>
<input type="checkbox" name="service" value="4">无自费<br/>


产品卖点：<br/>
<input type="text" id="selling1"><br/>
<input type="text" id="selling2"><br/>
<input type="text" id="selling3"><br/>


内容：<textarea rows="" cols="" id="content"></textarea><br/>
<input id="id" >
<input type="button" onclick="doSubmit()" value="提交">
<script src="js/jquery.min.js?v=2.1.4"></script>
<script src="js/bootstrap.min.js?v=3.3.6"></script>

    
<script type="text/javascript">
var imageURL="";
function doSubmit(){
	if(!confirm("您确定要修改吗？")){
		return
	}
	var id=$("#id").val();
	var price=$("#price").val();
	var name=$("#name").val();
	var descrip=$("#descrip").val();
	var tags=$("#tags").val();
	
	
	var services ="";
	$('input[name="service"]:checked').each(function(){
		 //service.push($(this).val());
		 services+=$(this).val()+",";
	});
	services = services.substr(0, services.length - 1);
	
	var selling1=$("#selling1").val();
	var selling2=$("#selling2").val();
	var selling3=$("#selling3").val();
	var content=$("#content").val();
	
	if (name==""){
		$("#name").focus();
		$("#name_msg").html("请输入名称！")
		return
	}else{
		$("#username_msg").html("")
	}
	
	$.ajax({   
           url:'goods/updateById',   
           type:'post',   
           dataType:'json',//服务器端返回的数据格式是json
		   data: {
			   "id":id,
			   "price":price,
			   "name":name,
			   "descrip":descrip,
			   "tags":tags,
			   "services":services,
			   "selling1":selling1,
			   "selling2":selling2,
			   "selling3":selling3,
			   "content":content,
			   "img":imageURL,
		   },//发给服务器端的数据
           success:function(res){   //data:服务器端返回给浏览器端的数据
		   		if(res.code==0){
		   			alert(res.msg);
		   		}else{
		   			alert(res.msg);
		   		}
           },
		   error:function (XMLHttpRequest, textStatus, errorThrown) {
        	   		alert(XMLHttpRequest);
        	   		alert(textStatus);
        	   		alert(errorThrown);
           }
           
       });
}

function upload(){
    var formData = new FormData();
    formData.append("image",$("#image")[0].files[0]);
    $.ajax({
        url:'goods/upload', /*接口域名地址*/
        type:'post',
        data: formData,
        contentType: false,
        processData: false,
        success:function(res){
            console.log(res.data);
            if(res.code==0){
                $("#img").attr('src',res.data.imageURL);
                imageURL=res.data.dbURL;
            }else{
                alert(res.msg);
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
	   		alert(XMLHttpRequest);
	   		alert(textStatus);
	   		alert(errorThrown);
   		}
    })
}

function getParam(variable){
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}
function init(){
	var id=getParam("id");
	$.ajax({
        url:'goods/showGoodsById', /*接口域名地址*/
        type:'get',
        data: {
        	"id":id
        },
        success:function(res){
            console.log(res.data);
            if(res.code==0){
            	$("#id").val(res.data.id);
            	$("#price").val(res.data.price);
                $("#name").val(res.data.name);
                $("#img").attr('src',"../"+res.data.img);
                imageURL=res.data.img;
                $("#descrip").val(res.data.descrip);
                $("#tags").val(res.data.tags);
                $("#selling1").val(res.data.selling1);
                $("#selling2").val(res.data.selling2);
                $("#selling3").val(res.data.selling3);
                $("#content").val(res.data.content);
                var services=res.data.services;
                if(services != null && services.trim().length > 0){
                	servicesArray=services.split(",")
                	console.log(servicesArray)
                	
                	for(var i=0;i<servicesArray.length;i++){
                		$('input[name="service"]').each(function(){
                			 //service.push($(this).val());
                			 services+=$(this).val()+",";

                     		if(servicesArray[i]==$(this).val()){
                     			$(this).prop('checked',true);
                     		}
                		});
                	}
                }
            }else{
                alert(res.msg);
            }
        },
        error:function (XMLHttpRequest, textStatus, errorThrown) {
	   		alert(XMLHttpRequest);
	   		alert(textStatus);
	   		alert(errorThrown);
   		}
    })
}

window.onload=init;
</script>
</body>
</html>